// pages/travel/travel.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    text: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var type = options.type;
    var text = options.text;
    console.log(text);
    if (type == undefined) {
      type = '';
    }
    if (text == undefined) {
      text = '';
 
    }
    //轮播图
    that.getList(type, text);
  }, 
  //添加搜索
  add: function (text){
    var that = this;
         that.setData({
        text:text
      })
    var userInfo = util.getKey("userInfo");
    httpUtil.getHttp("/search/user/add", {
      userId: userInfo.id, text: text
      }, function (res) {
      })
 
  },
  //获取列表
  getList: function (type, text) {
    var that = this;
    httpUtil.getHttp("/goods_travel/list", {
      type: type
    }, function(res) {
      var list = res;
      if(text!=''&&text!=undefined){
        for (var i = 0; i < list.length; i++) {
          var tx = list[i].name;
          if (tx.indexOf(text) == -1) {
            list[i].isDelete = 1;
          } else {
            list[i].isDelete = 0;
          }
        }
        that.add(text);

      }
      that.setData({
        goodsList: list
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  travelUrl: function(e) {
    var id = e.target.dataset.id;
    if (id == undefined) {
      return;
    }
    wx.navigateTo({
      url: '../proInfo/proInfo?id=' + id
    })
  },
  search: function(e) {
    var that = this;
    var text = e.detail.value;
    var list = that.data.goodsList;
    if (text != undefined && text != '') {
      for (var i = 0; i < list.length; i++) {
        var tx = list[i].name;
        if (tx.indexOf(text) == -1) {
          list[i].isDelete = 1;
        } else {
          list[i].isDelete = 0;
        }
      }
      that.setData({
        goodsList: list
      })
      // that.add(text);
    }

  }
})